package cn.zl.ymcc.service.impl;

import cn.zl.ymcc.constant.CodeConstant;
import cn.zl.ymcc.dto.CodeDto;
import cn.zl.ymcc.dto.RegisterDto;
import cn.zl.ymcc.entity.Login;
import cn.zl.ymcc.entity.User;
import cn.zl.ymcc.entity.UserAccount;
import cn.zl.ymcc.entity.UserBaseInfo;
import cn.zl.ymcc.exception.GlobalBusinessException;
import cn.zl.ymcc.fegin.UaaFegin;
import cn.zl.ymcc.mapper.UserMapper;
import cn.zl.ymcc.result.Result;
import cn.zl.ymcc.service.UserAccountService;
import cn.zl.ymcc.service.UserBaseInfoService;
import cn.zl.ymcc.service.UserService;
import cn.zl.ymcc.utils.PhoneValidator;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author zl
 * @since 2025-10-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UaaFegin uaaFegin;
    @Autowired
    private UserBaseInfoService userBaseInfoService;
    @Autowired
    private UserAccountService userAccountService;
    @GlobalTransactional
    @Override
    public boolean Register(RegisterDto registerDto) {
        String phone = registerDto.getMobile();
        boolean result = PhoneValidator.validateBasic(phone);
        if (!result) {
            throw new GlobalBusinessException("手机号格式错误");
        }
        Object value = redisTemplate.opsForValue().get(CodeConstant.PHONE_CODE_KEY + phone);
        CodeDto codeDto = (CodeDto) value;

        if (codeDto == null) {
            throw new GlobalBusinessException("验证码已过期");
        } else if (!codeDto.getCode().equals(registerDto.getSmsCode())) {
            throw new GlobalBusinessException("验证码错误");
        }
        //对密码格式进行校验
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        User user = this.getOne(queryWrapper);
        if (user != null) {
            throw new GlobalBusinessException("手机号已存在");
        }

        //user userBase userAccount login
        //login 表
        Login login = new Login();
        login.setUsername(phone);
        login.setPassword(registerDto.getPassword());
        login.setType(true);
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setCredentialsNonExpired(true);
        login.setAccountNonLocked(true);

        //user
        Result result1 = uaaFegin.register(login);

        Object data = result1.getData();


        long LoginId=(int)data;
        User user1 = new User();
        user1.setPhone(phone);
        user1.setLoginId(LoginId);
        user1.setNickName(phone);
        user1.setBitState(0l);
        user1.setSecLevel(new Byte("1"));

        save(user1);
        //userbase
        UserBaseInfo userBaseInfo = new UserBaseInfo();
       userBaseInfo.setId(user1.getId());
       userBaseInfo.setRegChannel(new Byte("1"));
       userBaseInfo.setSex(new Byte("1"));
       userBaseInfo.setBirthday(null);
       userBaseInfo.setAreaCode("110000");
       userBaseInfo.setAddress("中国");
       userBaseInfo.setLevel(new Byte("1"));
       userBaseInfo.setGrowScore(0);
       userBaseInfo.setReferId(0l);
       userBaseInfo.setCreateTime(System.currentTimeMillis());
       userBaseInfo.setUpdateTime(System.currentTimeMillis());

        userBaseInfoService.save(userBaseInfo);
        //user account
        UserAccount userAccount = new UserAccount();
        userAccount.setId(user1.getId());
        userAccount.setUsableAmount(0l);
        userAccount.setFrozenAmount(0l);
        userAccount.setCreateTime(System.currentTimeMillis());
        userAccount.setUpdateTime(System.currentTimeMillis());
        userAccount.setPassword(registerDto.getPassword());

        userAccountService.save(userAccount);


        return true;
    }
}
